package com.cw.jeeyt.service.impl.elasticsearch;

import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Service;

import com.cw.jeeyt.service.bo.elasticsearch.ElasticDocDto;
import com.cw.jeeyt.core.intf.ElasticSearchCore;
import com.cw.jeeyt.service.elasticsearch.IElasticService;

@Service("elasticService")
public class ElasticServiceImpl implements IElasticService {

    @Autowired
    private ElasticsearchRestTemplate elasticsearchTemplate;
    @Autowired
    private ElasticSearchCore elasticRepository;

    private Pageable pageable = PageRequest.of(0,10);

    @Override
    public void createIndex() {
        elasticsearchTemplate.indexOps(ElasticDocDto.class);
    }

    @Override
    public void deleteIndex(String index) {
        elasticsearchTemplate.delete(index);
    }

    @Override
    public void save(ElasticDocDto ElasticDocDto) {
        elasticRepository.save(ElasticDocDto);
    }

    @Override
    public void saveAll(List<ElasticDocDto> list) {
        elasticRepository.saveAll(list);
    }

    @Override
    public Iterator<ElasticDocDto> findAll() {
        return elasticRepository.findAll().iterator();
    }

    @Override
    public Page<ElasticDocDto> findByContent(String content) {
        return elasticRepository.findByContent(content,pageable);
    }

    @Override
    public Page<ElasticDocDto> findByFirstCode(String firstCode) {
        return elasticRepository.findByFirstCode(firstCode,pageable);
    }

    @Override
    public Page<ElasticDocDto> findBySecordCode(String secordCode) {
        return elasticRepository.findBySecordCode(secordCode,pageable);
    }

    @Override
    public Page<ElasticDocDto> query(String key) {
        return elasticRepository.findByContent(key,pageable);
    }
}
